package com.mouse.magent.web.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mouse.magent.common.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Aspect
@Configuration
@Slf4j
public class AopConfig {
    @Autowired
    ObjectMapper objectMapper;

    @Around("execution(* com.mouse.magent.web.controller..*(..))")
    public Object functionAccessCheck(final ProceedingJoinPoint pjp) throws Throwable {
        try {
            log.info("------Begin-----------:" + DateUtil.nowTime());
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            // 请求的IP
            String ip = getIpAddress(request);
            String url = request.getRequestURI();
            String targetName = pjp.getTarget().getClass().getName();
            String getMethod = request.getMethod();
            // 获取方法名
            String methodName = pjp.getSignature().getName();
            log.info("ip:{}\turl:{}\ttargetName:{}\tgetMethod:{}\tMethodName:{}", ip, url, targetName, getMethod, methodName);
            // 获取输入参数
            String requestContent = "";
            List<Object> listArgs = Arrays.asList(pjp.getArgs());
            try {
                requestContent = (new ObjectMapper()).writeValueAsString(listArgs);
            } catch (Exception e) {
            }
            log.info("Input args:" + requestContent);
            Object o = pjp.proceed();

            log.info("Output return :" + objectMapper.writeValueAsString(o));
            log.info("------End-----------:" + DateUtil.nowTime());
            return o;
        } catch (Throwable e) {
            throw e;
        }
    }

    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
